package com.itheima.controller;

import com.itheima.utils.MessageConstant;
import com.itheima.service.LoginService;
import com.itheima.utils.CheckUtils;
import com.itheima.utils.ResultUtil;
import com.itheima.utils.StringUtils;
import com.itheima.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录管理相关接口
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录管理相关接口")
@CrossOrigin
public class LoginController {


    @Autowired
    private LoginService loginService;


    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public String login(@RequestBody UserVO userVO, HttpServletRequest request) {
        try {
            if (org.apache.commons.lang.StringUtils.isEmpty(userVO.getUserName() )|| org.apache.commons.lang.StringUtils.isEmpty(userVO.getPassWord())){
                return ResultUtil.result(401,"请输入用户名或者密码");
            }
            String flag = loginService.login(userVO, request);
            if (flag==null) {
                return ResultUtil.result(401, MessageConstant.USER_LOGIN_FAIL);
            }
            return ResultUtil.result(200, "token:"+flag);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.USER_LOGIN_FAIL);
        }
    }

    //用户注册
    @PostMapping("/register")

    @ApiOperation("用户注册")
    public String register(@RequestBody UserVO userVO, HttpServletRequest request) {
        try {
            if (org.apache.commons.lang.StringUtils.isEmpty(userVO.getUserName() )|| org.apache.commons.lang.StringUtils.isEmpty(userVO.getPassWord())){
                return ResultUtil.result(401,"请输入用户名或者密码");
            }
            if (StringUtils.isEmpty(String.valueOf(userVO.getBirthday())) || !String.valueOf(userVO.getBirthday()).contains("-")){
                return ResultUtil.result(401, "日期格式请为yyyy-MM-dd");
            }
            if (!CheckUtils.checkEmail(userVO.getEmail())){
                //邮箱错误直接返回
                return ResultUtil.result(401, "邮箱格式错误");
            }
            if (!CheckUtils.checkMobileNumber(userVO.getMobile())){
                //手机号格式不对
                return ResultUtil.result(401, "手机号格式错误");
            }
            Boolean flag = loginService.register(userVO, request);
            if (!flag) {
                return ResultUtil.result(401, MessageConstant.USER_REGISTER_FAIL);
            }
            return ResultUtil.result(200, MessageConstant.USER_REGISTER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.USER_REGISTER_FAIL);
        }
    }

    @GetMapping("/active")

    @ApiOperation("激活账号")
    public String active(String code) {
        Boolean flag = loginService.active(code);

        if (flag) {
            return ResultUtil.result(401, MessageConstant.USER_ACTIVE_SUCCESS);
        }
        return ResultUtil.result(401, MessageConstant.USER_ACTIVE_FAIL);


    }
}

